<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title> - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content=" - 天地维杰网" />
    <meta name="description" content="天地维杰网 | 博客 | 软件 | 架构 | Java "> 
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7826003325059020" crossorigin="anonymous"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/mac/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/redis4.0%E5%88%A9%E7%94%A8%E7%AE%A1%E9%81%93%E4%BC%98%E5%8C%96aofrewrite/" itemprop="url">
        
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="0001-01-01">
    0001-01-01
</time>
</span> 
      
      
       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">1332 字 ~7分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    

<h1 id="redis4-0之利用管道优化aofrewrite">redis4.0之利用管道优化aofrewrite</h1>

<blockquote>
<p>原文地址 <a href="https://developer.aliyun.com/article/177819">https://developer.aliyun.com/article/177819</a></p>
</blockquote>

<p><strong>简介：</strong> redis的aof持久化本质上是一个redo log，把所有执行过的写命令追加到aof文件中。那么随着redis的运行，aof文件会不断膨胀，当触发收缩条件时就要做aofrewrite。 redis是通过fork子进程来做aofrewrite，同时为了保证aof的连续性，父进程把aofrewrite期间的写命令缓存起来，等收割完子进程之后再追加到新的aof文件。</p>

<h1 id="前言">前言</h1>

<p>redis的aof持久化本质上是一个redo log，把所有执行过的写命令追加到aof文件中。那么随着redis的运行，aof文件会不断膨胀，当触发收缩条件时就要做aofrewrite。</p>

<p>redis是通过fork子进程来做aofrewrite，同时为了保证aof的连续性，父进程把aofrewrite期间的写命令缓存起来，等收割完子进程之后再追加到新的aof文件。如果期间写入量较大的话收割时就要有大量的写磁盘操作，造成性能下降。</p>

<p>为了提高aofrewrite效率，redis通过在父子进程间建立管道，把aofrewrite期间的写命令通过管道同步给子进程，追加写盘的操作也就转交给了子进程。其实利用管道的优化在3.0时期就已经实现了，最近在总结4.0的新特性，索性就都归到4.0里，方便查阅。</p>

<h1 id="aofrewrite详解">aofrewrite详解</h1>

<h2 id="1-aofrewrite的基础实现">1. aofrewrite的基础实现</h2>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220616153238.png" width = 900 /> </div>

<p>上图是aofrewrite的流程，标注为基本的函数调用关系。</p>

<ul>
<li>1 - 首先，通过命令或是事件触发aofrewrite，调用rewriteAppendOnlyFileBackground()函数

<ul>
<li>该函数会fork出一个子进程</li>
</ul></li>
<li>2 - 父进程记录子进程的pid并开始缓存写命令

<ul>
<li>当pid不为-1时就会执行aofRewriteBufferAppend()把写命令缓存起来</li>
</ul></li>
<li>3 - 子进程调用rewriteAppendOnlyFile(tmpfile)函数创建新的aof文件

<ul>
<li>调用rewriteAppendOnlyFileRio()函数遍历redis把所有key-value以命令的方式写入新aof文件</li>
<li>完成后调用exitFromChild(0)退出</li>
</ul></li>
<li>4 - 子进程退出后父进程调用backgroundRewriteDoneHandler()来处理

<ul>
<li>调用aofRewriteBufferWrite()函数把积攒的写命令缓存写入子进程创建的临时aof文件</li>
<li>最后rename()用新的aof文件替换掉原来的aof文件</li>
</ul></li>
</ul>

<p>在aofrewrite过程中，如果redis本身数据量较大子进程执行时间较长，或者写入流量较高，就会导致aof-rewrite-buffer积攒较多，父进程就要进行大量写磁盘操作，这对于redis来说显然是不够高效的。</p>

<h2 id="2-使用pipe优化">2. 使用pipe优化</h2>

<p>为了提高aofrewrite效率，redis使用pipe来优化，下图中红色标注即为优化的部分：</p>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220616153312.png" width = 900 /> </div>

<p>优化点：</p>

<ul>
<li>1 - 父进程建立管道

<ul>
<li>共三条管道，分别为一条数据管道，和两条控制管道</li>
<li>数据管道用来传输数据，控制管道用来做父子进程交互，控制何时停止数据传输</li>
</ul></li>
<li>2 - 父进程向管道写数据

<ul>
<li>注册写事件aofChildWriteDiffData()向数据管道写数据</li>
</ul></li>
<li>3 - 子进程从管道读数据

<ul>
<li>子进程在生成新aof文件时会定期调用aofReadDiffFromParent()从管道读取数据，并缓存下来</li>
</ul></li>
<li>4 - 父子进程交互

<ul>
<li>子进程生成新aof文件后会通过控制管道向父进程发送&rdquo;!&ldquo;，发起停止数据传输请求</li>
<li>父进程收到停止信号后激活读事件处理函数aofChildPipeReadable()，设置server.aof_stop_sending_diff=1停止数据传输，并向子进程回复&rdquo;!&ldquo;，表示同意停止</li>
<li>子进程收到父进程的应答，调用rioWrite()把积攒的数据追加到新的aof文件，最后退出</li>
</ul></li>
</ul>

<p>细心的读者会发现，aofRewriteBufferAppend()和aofRewriteBufferWrite()这一对函数仍然保留，父进程还是要把aof-rewrite-buffer写盘吗？是的，这是因为父子进程是异步结构，父子间总会有那么一点代沟，aof-rewrite-buffer还是需要保留的，不过这个时候父进程写盘的数据量就很小了，几乎可以忽略。</p>

<h2 id="3-aofrewrite代码剖析">3. aofrewrite代码剖析</h2>

<h3 id="aofrewrite的触发条件">aofrewrite的触发条件</h3>

<ul>
<li>1. 执行bgrewriteaof命令。</li>
<li>2. serverCron时间事件检测到aof文件大小超限。</li>
</ul>

<p>命令的触发不必详述，主要来看下serverCron的触发：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
    ...
         /* Trigger an AOF rewrite if needed */
         if (server.rdb_child_pid == -1 &amp;&amp;
             server.aof_child_pid == -1 &amp;&amp;
             server.aof_rewrite_perc &amp;&amp;
             server.aof_current_size &gt; server.aof_rewrite_min_size)
         {
            long long base = server.aof_rewrite_base_size ?
                            server.aof_rewrite_base_size : 1;
            long long growth = (server.aof_current_size*100/base) - 100;
            if (growth &gt;= server.aof_rewrite_perc) {
                serverLog(LL_NOTICE,&#34;Starting automatic rewriting of AOF on %lld%% growth&#34;,growth);
                rewriteAppendOnlyFileBackground();
            }
         }
    ...
}</code></pre></div>
<p>也就是说aof文件大小超过了server.aof_rewrite_min_size，并且增长率大于server.aof_rewrite_perc时就会触发（增长率计算的基数server.aof_rewrite_base_size是上次aofrewrite完之后aof文件的大小）。</p>

<p>目前云redis设置server.aof_rewrite_min_size为内存规格的1/4，server.aof_rewrite_perc为100。</p>

<h3 id="管道建立">管道建立</h3>

<p>aofrewrite触发之后进入rewriteAppendOnlyFileBackground()函数：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">int rewriteAppendOnlyFileBackground(void) {
    pid_t childpid;
    long long start;

    if (server.aof_child_pid != -1 || server.rdb_child_pid != -1) return C_ERR;
    if (aofCreatePipes() != C_OK) return C_ERR;
    openChildInfoPipe();
    start = ustime();
    if ((childpid = fork()) == 0) {
    ...</code></pre></div>
<p>OK，重点来了，在fork之前调用了aofCreatePipes()函数来创建管道（openChildInfoPipe()函数只是用来收集子进程copy-on-write用到的内存，就不详细展开了）：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">int aofCreatePipes(void) {
    int fds[6] = {-1, -1, -1, -1, -1, -1};
    int j;

    if (pipe(fds) == -1) goto error;   /* parent -&gt; children data. 父进程向子进程写数据的管道*/
    if (pipe(fds+2) == -1) goto error; /* children -&gt; parent ack.  子进程向父进程发起停止传输的控制管道*/
    if (pipe(fds+4) == -1) goto error; /* parent -&gt; children ack.  父进程向子进程回复的控制管道*/
    /* Parent -&gt; children data is non blocking. */
    if (anetNonBlock(NULL,fds[0]) != ANET_OK) goto error;
    if (anetNonBlock(NULL,fds[1]) != ANET_OK) goto error;
    if (aeCreateFileEvent(server.el, fds[2], AE_READABLE, aofChildPipeReadable, NULL) == AE_ERR) goto error;
    //注册读事件处理函数，负责处理子进程要求停止数据传输的消息

    server.aof_pipe_write_data_to_child = fds[1];      //父进程向子进程写数据的fd
    server.aof_pipe_read_data_from_parent = fds[0];    //子进程从父进程读数据的fd
    server.aof_pipe_write_ack_to_parent = fds[3];      //子进程向父进程发起停止消息的fd
    server.aof_pipe_read_ack_from_child = fds[2];      //父进程从子进程读取停止消息的fd
    server.aof_pipe_write_ack_to_child = fds[5];       //父进程向子进程回复消息的fd
    server.aof_pipe_read_ack_from_parent = fds[4];     //子进程从父进程读取回复消息的fd
    server.aof_stop_sending_diff = 0;                  //是否停止管道传输标记位
    return C_OK;
    ...
}</code></pre></div>
<h3 id="父进程与管道传输">父进程与管道传输</h3>

<p>管道建立起来了我们再来看看fork之后父进程和子进程如何工作，首先看下父进程：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">        /* Parent */
        server.stat_fork_time = ustime()-start;
        server.stat_fork_rate = (double) zmalloc_used_memory() * 1000000 / server.stat_fork_time / (1024*1024*1024); /* GB per second. */
        latencyAddSampleIfNeeded(&#34;fork&#34;,server.stat_fork_time/1000);
        ...
        server.aof_rewrite_scheduled = 0;
        server.aof_rewrite_time_start = time(NULL);
        server.aof_child_pid = childpid;
        updateDictResizePolicy();
        /* We set appendseldb to -1 in order to force the next call to the
         * feedAppendOnlyFile() to issue a SELECT command, so the differences
         * accumulated by the parent into server.aof_rewrite_buf will start
         * with a SELECT statement and it will be safe to merge. */
        server.aof_selected_db = -1;
        ...</code></pre></div>
<p>父进程这里做的事情并不多，主要是信息的记录和一些标记位设置</p>

<ul>
<li>记录fork消耗的时间，info命令可以查看上次fork的耗时latest_fork_usec，单位微秒</li>
<li>设置server.aof_rewrite_scheduled = 0，防止serverCron再次触发aofrewrite</li>
<li>设置server.aof_child_pid为子进程pid，其不为-1时redis才会向aof-rewrite-buffer缓存写命令</li>
<li>updateDictResizePolicy()禁止所有hash数据结构resize，这是为了尽量避免子进程copy-on-write进行内存拷贝</li>
<li>设置server.aof_selected_db = -1，下一次的aof日志会强制加上select，这是为了保证命令执行到正确的db</li>
</ul>

<p>接下来就是缓存写命令和管道通信部分了，入口是在feedAppendOnlyFile()：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc) {
    ...
    if (server.aof_child_pid != -1)
        aofRewriteBufferAppend((unsigned char*)buf,sdslen(buf));
    ...
}</code></pre></div>
<p>server.aof_child_pid在这时就生效了，开始缓存写命令：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">void aofRewriteBufferAppend(unsigned char *s, unsigned long len) {
    listNode *ln = listLast(server.aof_rewrite_buf_blocks);
    aofrwblock *block = ln ? ln-&gt;value : NULL;

    while(len) {
        /* If we already got at least an allocated block, try appending
         * at least some piece into it. */
        if (block) {
            unsigned long thislen = (block-&gt;free &lt; len) ? block-&gt;free : len;
            if (thislen) {  /* The current block is not already full. */
                memcpy(block-&gt;buf+block-&gt;used, s, thislen);
                block-&gt;used += thislen;
                block-&gt;free -= thislen;
                s += thislen;
                len -= thislen;
            }
        }

        if (len) { /* First block to allocate, or need another block. */
            int numblocks;

            block = zmalloc(sizeof(*block));
            block-&gt;free = AOF_RW_BUF_BLOCK_SIZE;
            block-&gt;used = 0;
            listAddNodeTail(server.aof_rewrite_buf_blocks,block);

            /* Log every time we cross more 10 or 100 blocks, respectively
             * as a notice or warning. */
            numblocks = listLength(server.aof_rewrite_buf_blocks);
            if (((numblocks+1) % 10) == 0) {
                int level = ((numblocks+1) % 100) == 0 ? LL_WARNING :
                                                         LL_NOTICE;
                serverLog(level,&#34;Background AOF buffer size: %lu MB&#34;,
                    aofRewriteBufferSize()/(1024*1024));
            }
        }
    }
    if (aeGetFileEvents(server.el,server.aof_pipe_write_data_to_child) == 0) {
        aeCreateFileEvent(server.el, server.aof_pipe_write_data_to_child,
            AE_WRITABLE, aofChildWriteDiffData, NULL);
    }
}</code></pre></div>
<p>redis用链表server.aof_rewrite_buf_blocks来缓存aofrewrite期间的写命令，链表的每个节点最大10MB；重点是在最后的写事件注册，当server.aof_pipe_write_data_to_child这个fd没有注册事件时，就注册写事件函数aofChildWriteDiffData：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">void aofChildWriteDiffData(aeEventLoop *el, int fd, void *privdata, int mask) {
    listNode *ln;
    aofrwblock *block;
    ssize_t nwritten;
    ...
    while(1) {
        ln = listFirst(server.aof_rewrite_buf_blocks);
        block = ln ? ln-&gt;value : NULL;
        if (server.aof_stop_sending_diff || !block) {
            aeDeleteFileEvent(server.el,server.aof_pipe_write_data_to_child,
                              AE_WRITABLE);
            return;
        }
        if (block-&gt;used &gt; 0) {
            nwritten = write(server.aof_pipe_write_data_to_child,
                             block-&gt;buf,block-&gt;used);
            if (nwritten &lt;= 0) return;
            memmove(block-&gt;buf,block-&gt;buf+nwritten,block-&gt;used-nwritten);
            block-&gt;used -= nwritten;
            block-&gt;free += nwritten;
        }
        if (block-&gt;used == 0) listDelNode(server.aof_rewrite_buf_blocks,ln);
    }
}</code></pre></div>
<p>每次事件循环都会把server.aof_rewrite_buf_blocks积攒的写命令全部同步给子进程，除非server.aof_stop_sending_diff被设置了停止标记。</p>

<h3 id="子进程和管道传输">子进程和管道传输</h3>

<p>接下来看下子进程：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">...
        /* Child */
        char tmpfile[256];
        closeListeningSockets(0);
        redisSetProcTitle(&#34;redis-aof-rewrite&#34;);
        snprintf(tmpfile,256,&#34;temp-rewriteaof-bg-%d.aof&#34;, (int) getpid());
        if (rewriteAppendOnlyFile(tmpfile) == C_OK) {
            ...
            exitFromChild(0);
        } else {
            exitFromChild(1);
        }
...</code></pre></div>
<p>子进程首先关闭监听端口，然后就进入rewriteAppendOnlyFile()函数：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">int rewriteAppendOnlyFile(char *filename) {
    ...
    snprintf(tmpfile,256,&#34;temp-rewriteaof-%d.aof&#34;, (int) getpid());
    fp = fopen(tmpfile,&#34;w&#34;);
    ...
    server.aof_child_diff = sdsempty();
    ...
        if (rewriteAppendOnlyFileRio(&amp;aof) == C_ERR) goto werr;
    ...</code></pre></div>
<p>首先打开一个临时aof文件，并初始化server.aof_child_diff缓存准备从父进程读数据，然后就调用rewriteAppendOnlyFileRio()来写aof文件和读取管道中的数据：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">int rewriteAppendOnlyFileRio(rio *aof) {
    ...
            if (aof-&gt;processed_bytes &gt; processed+AOF_READ_DIFF_INTERVAL_BYTES) {
                processed = aof-&gt;processed_bytes;
                aofReadDiffFromParent();
            }
    ...
}</code></pre></div>
<p>在遍历redis把key-value写入新aof文件过程中，新aof文件每增长10K就会调用aofReadDiffFromParent()从管道中读取数据追加到server.aof_child_diff：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">ssize_t aofReadDiffFromParent(void) {
    char buf[65536]; /* Default pipe buffer size on most Linux systems. */
    ssize_t nread, total = 0;

    while ((nread =
            read(server.aof_pipe_read_data_from_parent,buf,sizeof(buf))) &gt; 0) {
        server.aof_child_diff = sdscatlen(server.aof_child_diff,buf,nread);
        total += nread;
    }
    return total;
}</code></pre></div>
<h3 id="停止管道传输">停止管道传输</h3>

<p>子进程在遍历完redis生成好新的aof文件之后就要准备退出了，那么退出前要先告诉父进程停止管道传输，依然回到rewriteAppendOnlyFile()函数来看：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">int rewriteAppendOnlyFile(char *filename) {
    ...
    /* Ask the master to stop sending diffs. */
    if (write(server.aof_pipe_write_ack_to_parent,&#34;!&#34;,1) != 1) goto werr;
    if (anetNonBlock(NULL,server.aof_pipe_read_ack_from_parent) != ANET_OK)
        goto werr;
    /* We read the ACK from the server using a 10 seconds timeout. Normally
     * it should reply ASAP, but just in case we lose its reply, we are sure
     * the child will eventually get terminated. */
    if (syncRead(server.aof_pipe_read_ack_from_parent,&amp;byte,1,5000) != 1 ||
        byte != &#39;!&#39;) goto werr;
    serverLog(LL_NOTICE,&#34;Parent agreed to stop sending diffs. Finalizing AOF...&#34;);

    /* Read the final diff if any. */
    aofReadDiffFromParent();

    /* Write the received diff to the file. */
    serverLog(LL_NOTICE,
        &#34;Concatenating %.2f MB of AOF diff received from parent.&#34;,
        (double) sdslen(server.aof_child_diff) / (1024*1024));
    if (rioWrite(&amp;aof,server.aof_child_diff,sdslen(server.aof_child_diff)) == 0)
        goto werr;

    /* Make sure data will not remain on the OS&#39;s output buffers */
    if (fflush(fp) == EOF) goto werr;
    if (fsync(fileno(fp)) == -1) goto werr;
    if (fclose(fp) == EOF) goto werr;
    ...
}</code></pre></div>
<p>这里写的就很直接了：</p>

<ul>
<li>使用write向控制管道写入&rdquo;!&ldquo;发起停止请求，然后读取返回结果，超时时间为10s</li>
<li>超时就goto werr异常退出，10s内读取到&rdquo;!&ldquo;就继续</li>
<li>再次调用aofReadDiffFromParent()从数据管道读取数据确保管道中没有遗留</li>
<li>最后rioWrite()把server.aof_child_diff积攒的数据追加到新的aof文件</li>
</ul>

<p>那么父进程是如何处理&rdquo;!&ldquo;的呢，还记得之前注册的读事件aofChildPipeReadable()吧，子进程向控制管道发送&rdquo;!&ldquo;就会激活：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">void aofChildPipeReadable(aeEventLoop *el, int fd, void *privdata, int mask) {
    char byte;
    ...
    if (read(fd,&amp;byte,1) == 1 &amp;&amp; byte == &#39;!&#39;) {
        serverLog(LL_NOTICE,&#34;AOF rewrite child asks to stop sending diffs.&#34;);
        server.aof_stop_sending_diff = 1;
        if (write(server.aof_pipe_write_ack_to_child,&#34;!&#34;,1) != 1) {
            /* If we can&#39;t send the ack, inform the user, but don&#39;t try again
             * since in the other side the children will use a timeout if the
             * kernel can&#39;t buffer our write, or, the children was
             * terminated. */
            serverLog(LL_WARNING,&#34;Can&#39;t send ACK to AOF child: %s&#34;,
                strerror(errno));
        }
    }
    /* Remove the handler since this can be called only one time during a
     * rewrite. */
    aeDeleteFileEvent(server.el,server.aof_pipe_read_ack_from_child,AE_READABLE);
}</code></pre></div>
<p>很简单，标记server.aof_stop_sending_diff=1，给子进程回复&rdquo;!&ldquo;，并且把自己从事件循环删掉，自此父子进程间通信完成，剩下的就是父进程等待子进程退出进行收尾工作。</p>

<h3 id="父进程收尾">父进程收尾</h3>

<p>serverCron()中会调用wait3()来收割子进程：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">    /* Check if a background saving or AOF rewrite in progress terminated. */
    if (server.rdb_child_pid != -1 || server.aof_child_pid != -1 ||
        ldbPendingChildren())
    {
        int statloc;
        pid_t pid;

        if ((pid = wait3(&amp;statloc,WNOHANG,NULL)) != 0) {
            int exitcode = WEXITSTATUS(statloc);
            int bysignal = 0;

            if (WIFSIGNALED(statloc)) bysignal = WTERMSIG(statloc);

            if (pid == -1) {
                serverLog(LL_WARNING,&#34;wait3() returned an error: %s. &#34;
                    &#34;rdb_child_pid = %d, aof_child_pid = %d&#34;,
                    strerror(errno),
                    (int) server.rdb_child_pid,
                    (int) server.aof_child_pid);
            } else if (pid == server.rdb_child_pid) {
                backgroundSaveDoneHandler(exitcode,bysignal);
                if (!bysignal &amp;&amp; exitcode == 0) receiveChildInfo();
            } else if (pid == server.aof_child_pid) {
                backgroundRewriteDoneHandler(exitcode,bysignal);
                if (!bysignal &amp;&amp; exitcode == 0) receiveChildInfo();
            } else {
                if (!ldbRemoveChild(pid)) {
                    serverLog(LL_WARNING,
                        &#34;Warning, detected child with unmatched pid: %ld&#34;,
                        (long)pid);
                }
            }
            updateDictResizePolicy();
            closeChildInfoPipe();
        }</code></pre></div>
<p>如果收割到的pid是server.aof_child_pid就进入backgroundRewriteDoneHandler()：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">void backgroundRewriteDoneHandler(int exitcode, int bysignal) {
    ...
        /* Flush the differences accumulated by the parent to the
         * rewritten AOF. */
        latencyStartMonitor(latency);
        snprintf(tmpfile,256,&#34;temp-rewriteaof-bg-%d.aof&#34;,
            (int)server.aof_child_pid);
        newfd = open(tmpfile,O_WRONLY|O_APPEND);
        if (newfd == -1) {
            serverLog(LL_WARNING,
                &#34;Unable to open the temporary AOF produced by the child: %s&#34;, strerror(errno));
            goto cleanup;
        }

        if (aofRewriteBufferWrite(newfd) == -1) {
            serverLog(LL_WARNING,
                &#34;Error trying to flush the parent diff to the rewritten AOF: %s&#34;, strerror(errno));
            close(newfd);
            goto cleanup;
        }
        latencyEndMonitor(latency);
        latencyAddSampleIfNeeded(&#34;aof-rewrite-diff-write&#34;,latency);</code></pre></div>
<p>首先会打开子进程生成的新aof文件，并调用aofRewriteBufferWrite()把server.aof_rewrite_buf_blocks中剩余的数据追加到新aof文件。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">        /* Rename the temporary file. This will not unlink the target file if
         * it exists, because we reference it with &#34;oldfd&#34;. */
        latencyStartMonitor(latency);
        if (rename(tmpfile,server.aof_filename) == -1) {
            serverLog(LL_WARNING,
                &#34;Error trying to rename the temporary AOF file %s into %s: %s&#34;,
                tmpfile,
                server.aof_filename,
                strerror(errno));
            close(newfd);
            if (oldfd != -1) close(oldfd);
            goto cleanup;
        }
        latencyEndMonitor(latency);
        latencyAddSampleIfNeeded(&#34;aof-rename&#34;,latency);</code></pre></div>
<p>之后把新aof文件rename为server.aof_filename记录的文件名。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">        /* Asynchronously close the overwritten AOF. */
        if (oldfd != -1) bioCreateBackgroundJob(BIO_CLOSE_FILE,(void*)(long)oldfd,NULL,NULL);</code></pre></div>
<p>使用bio后台线程来close原来的aof文件。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">cleanup:
    aofClosePipes();
    aofRewriteBufferReset();
    aofRemoveTempFile(server.aof_child_pid);
    server.aof_child_pid = -1;
    server.aof_rewrite_time_last = time(NULL)-server.aof_rewrite_time_start;
    server.aof_rewrite_time_start = -1;
    /* Schedule a new rewrite if we are waiting for it to switch the AOF ON. */
    if (server.aof_state == AOF_WAIT_REWRITE)
        server.aof_rewrite_scheduled = 1;</code></pre></div>
<p>最后是清理工作，包括关闭管道、重置aof-rewrite-buffer、复位server.aof_child_pid=-1等，自此aofrewrite完成。</p>

    </div>
    <footer class="post-footer">
     

     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis4.0-psync2%E4%BB%8B%E7%BB%8D/" rel="next" title="">
        <i class="fa fa-chevron-left"></i> 
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis4.0%E6%96%B0%E7%89%B9%E6%80%A7%E4%B8%80-memory-command/" rel="prev" title="">
         <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

    <section class="site-overview sidebar-panel  sidebar-panel-active ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">181</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">14</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">213</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2023</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>